package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.exception.ControllerException;
import com.cloudoer.blog.model.Filed;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/filed")
public class FiledController extends BaseController {

    @RequestMapping(value = "/index")
    public String index() {
        return "filed/index";
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map getByPage(@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<Filed> list = filedService.getByPage((pno-1)*psize, psize);
        return result(list, filedService.getCount(), "");
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ResponseBody
    public Map getAll (String filetypeId){
        List<Filed> list = filedService.getAll(filetypeId);
        return result(list, list.size(), "");
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ResponseBody
    public Filed getById (String id){
        return filedService.getById(id);
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map upload (MultipartFile file, String filetypeId) throws ServletException, IOException {
        filedService.add(file, filetypeId);
        return result(SUCCESS, "上传成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map update (Filed filed){
        filedService.update(filed);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete (String id){
        filedService.delete(id);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/download")
    public void download (String id, HttpServletResponse response) throws IOException {
        Filed filed = filedService.getById(id);
        if (filed == null){
            throw new ControllerException("文件不存在");
        }
        File file = fileUtil.getFile(filed.getUrl());
        response.setHeader("content-disposition", "attachment;filename=" + filed.getName());
        InputStream in = new FileInputStream(file);
        int len = 0;
        // 5.创建数据缓冲区
        byte[] buffer = new byte[1024];
        // 6.通过response对象获取OutputStream流
        OutputStream out = response.getOutputStream();
        // 7.将FileInputStream流写入到buffer缓冲区
        while ((len = in.read(buffer)) > 0) {
            // 8.使用OutputStream将缓冲区的数据输出到客户端浏览器
            out.write(buffer, 0, len);
        }
        in.close();
    }
}
